package com.example.cma

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTransaction
import com.ananth.frontrearcamera.fragment.CameraFragment


class MainActivity : AppCompatActivity() {

//    private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>
//    private lateinit var previewView: PreviewView
//    private lateinit var previewView2: PreviewView
//
//    private lateinit var cameraProviderFuture2: ListenableFuture<ProcessCameraProvider>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        previewView= findViewById(R.id.previewView)
//        previewView2= findViewById(R.id.previewView2)


        findViewById<View>(R.id.btnQx).setOnClickListener {
            Permission.checkPermission(this)
        }

        findViewById<View>(R.id.btn1).setOnClickListener {
            val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
            transaction.add(R.id.previewView,BlankFragment.newInstance("LENS_FACING_FRONT",""))
            transaction.commitAllowingStateLoss()
        }

        findViewById<View>(R.id.btn2).setOnClickListener {

            val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
            transaction.add(R.id.previewView2,BlankFragment.newInstance("LENS_FACING_BACK",""))
            transaction.commitAllowingStateLoss()

        }
        findViewById<View>(R.id.btn3).setOnClickListener {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, CameraFragment.newInstance())
                .commit()
        }

//        findViewById<View>(R.id.btn1).setOnClickListener {
//            cameraProviderFuture = ProcessCameraProvider.getInstance(this)//获得provider实例
//            //默认你会同意权限，不同意就是自己的事了
//            cameraProviderFuture.addListener(Runnable {
//                val cameraProvider = cameraProviderFuture.get()
//                bindPreview(cameraProvider)
//            }, ContextCompat.getMainExecutor(this))
//
//        }
//
//        findViewById<View>(R.id.btn2).setOnClickListener {
//            cameraProviderFuture2 = ProcessCameraProvider.getInstance(this)//获得provider实例
//            //默认你会同意权限，不同意就是自己的事了
//            cameraProviderFuture2.addListener(Runnable {
//                val cameraProvider = cameraProviderFuture2.get()
//                bindPreviewFront(cameraProvider)
//            }, ContextCompat.getMainExecutor(this))
//
//        }


    }



//    /**
//     * 绑定预览view
//     */
//    fun bindPreview(cameraProvider: ProcessCameraProvider) {
//        var preview: Preview = Preview.Builder()
//            .build()
//
//        var cameraSelector: CameraSelector = CameraSelector.Builder()
//            .requireLensFacing(CameraSelector.LENS_FACING_BACK)
//            .build()
//
//        preview.setSurfaceProvider(previewView.getSurfaceProvider())
//        var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
//
//
//
//        var preview2: Preview = Preview.Builder()
//            .build()
//
//        var cameraSelector2: CameraSelector = CameraSelector.Builder()
//            .requireLensFacing(CameraSelector.LENS_FACING_FRONT)
//            .build()
//        preview2.setSurfaceProvider(previewView2.getSurfaceProvider())
//        var camera2 = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector2, preview2)
//    }
//
//
////    https://developer.android.google.cn/training/camerax/preview?hl=zh-cn#get-provider
//    /**
//     * 绑定预览view
//     */
//    fun bindPreviewFront(cameraProvider: ProcessCameraProvider) {
//        var preview: Preview = Preview.Builder()
//            .build()
//
//        var cameraSelector: CameraSelector = CameraSelector.Builder()
//            .requireLensFacing(CameraSelector.LENS_FACING_FRONT)
//            .build()
//
//        preview.setSurfaceProvider(previewView2.getSurfaceProvider())
//
//        var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
//
//
//    }
//



}